package bootstrap

import (
	"card/app/framework/socket"
	"card/pkg/server/ws"
	"github.com/gin-gonic/gin"
	"log"
	"strings"
)

func SetUpWs(router *gin.Engine) {
	hub := socket.NewHub()
	go hub.ClientReadChanHandler()
	router.GET("/ws", func(c *gin.Context) {
		gameType := c.Query("game_type")
		if gameType == "" {
			return
		}
		conn, err := ws.ServeWS(c)
		if err != nil {
			log.Fatal(err)
		}
		client := socket.NewClient(conn, hub)
		hub.RegisterHandler(socket.Register(strings.ToLower(gameType)))
		hub.AddClient(client)
	})
}
